package db_servicer

import (
	"fmt"

	"gopkg.in/rana/ora.v3"
)

// 应该将报错信息 的生成，集中在一处，最佳位置是数据库存贮过程
// 故，建议返回值结构增加一项：　ｆａｌｇ　ｉｎｔ８，　ｍｓｇ　ｓｔｒｉｎｇ，　ｄａｔａ　　ｓｔｒｉｎｇ

func New_Pond(user_id int, pond_name string, area, height float64, prod string, density float64, source string, earth_thickness float64) dev_retMsg {

	var v_flag int8
	var v_ret_msg, v_data_msg string
	// logger = log.New(os.Stdout, ">>>>>>>DB.New Pond:", log.LstdFlags|log.Lshortfile)

	if _, err := ses.PrepAndExe("CALL pkg_pond_info.new_pond(:p1,:p2,:p3,:p4,:p5, :p6, :p7, :p8, :p9, :p10, :p11  )",
		&v_flag, &v_ret_msg, &v_data_msg, int32(user_id), pond_name, area, height, prod, density, source, earth_thickness); err != nil {

		logger.Panicf("Panic, retMsg:%v, Error:%v\n", v_ret_msg, err)
	}

	//	logger.Println("\n>>>pond_name:", pond_name,  ", Msg:", v_ret_msg, "Data:", v_data_msg)

	return dev_retMsg{Flag: v_flag,
		Message: v_ret_msg,
		Data:    v_data_msg,
	}

}

func Pond_List(p_user_id int) dev_retMsg {
	// logger = log.New(os.Stdout, ">>>>>>>DB.Pond list:", log.LstdFlags|log.Lshortfile)

	var v_flag int8
	var v_ret_msg string

	procRset := &ora.Rset{}

	stmtRefCall, err := ses.Prep("CALL  pkg_pond_info.pond_list(:p1,:p2, :p3, :p4)")
	defer stmtRefCall.Close()
	if err != nil {
		logger.Panic(fmt.Sprintf("Panic, Error:%v\n", err))
	}

	_, err = stmtRefCall.Exe(&v_flag, &v_ret_msg, procRset, int32(p_user_id))
	if err != nil {
		logger.Panicf("Panic, retMsg:%v, Error:%v\n", v_ret_msg, err)
	}

	//	logger.Printf(">>> rowsAffected:%v, flag: %v, Msg: %v, procRset[%T]: %v",rowsAffected, v_flag,  v_ret_msg, procRset, procRset)
	//
	return Refcursor_to_string(v_flag, v_ret_msg, &procRset)

}

func Modify_Pond(p_user_id, pond_id int, pond_name, prod, source string, area, height, density, earth_thickness float64) dev_retMsg {

	var v_flag int8
	var v_ret_msg, v_data_msg string

	// logger = log.New(os.Stdout, ">>>>>>>DB.Modify Pond:", log.LstdFlags|log.Lshortfile)

	if _, err := ses.PrepAndExe("CALL pkg_pond_info.modify_pond(:p1,:p2 , :p3, :p4 , :p5, :p6, :p7, :p8, :p9, :p10, :p11, :p12 )",
		&v_flag, &v_ret_msg, &v_data_msg, int32(p_user_id), int32(pond_id),
		pond_name, prod, source, area, height, density, earth_thickness); err != nil {

		logger.Panicf("Panic, retMsg:%v, Error:%v\n", v_ret_msg, err)
	}

	//	logger.Println("user_id:", p_user_id , ",flag:", v_flag,  ",Msg:", v_ret_msg, "Data:", v_data_msg)

	return dev_retMsg{Flag: v_flag,
		Message: v_ret_msg,
		Data:    v_data_msg,
	}

}

func Revoke_Pond(p_user_id, p_pond_id int) dev_retMsg {

	var v_flag int8
	var v_ret_msg, v_data_msg string

	// logger = log.New(os.Stdout, ">>>>>>>DB.Revoke Pond from User:", log.LstdFlags|log.Lshortfile)

	if _, err := ses.PrepAndExe("CALL pkg_pond_info.revoke_pond(:p1,:p2 , :p3, :p4 , :p5)",
		&v_flag, &v_ret_msg, &v_data_msg, int32(p_user_id), int32(p_pond_id)); err != nil {

		logger.Panicf("Panic, retMsg:%v, Error:%v\n", v_ret_msg, err)
	}

	//	logger.Println("user_id:", p_user_id , ",flag:", v_flag,  ",Msg:", v_ret_msg, "Data:", v_data_msg)

	return dev_retMsg{Flag: v_flag,
		Message: v_ret_msg,
		Data:    v_data_msg,
	}

}
